package com.leonzx.base.service.system;

import com.leonzx.base.dao.system.UserRoleTempDAO;
import com.leonzx.base.entity.system.UserRoleTemp;
import com.leonzx.base.service.BaseService;
import com.leonzx.base.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @Author:张勇
 * @Description:
 * @Date: 2018-06-19 17:47
 */
@Service
public class UserRoleTempService extends BaseService {
    @Autowired
    private UserRoleTempDAO userRoleTempDAO;

    /**
     * 保存或者更新临时权限表
     *
     * @param userRoleTemp
     */
    public void saveOrUpdate(UserRoleTemp userRoleTemp) {
        UserRoleTemp userRoleTempExample = new UserRoleTemp();
        userRoleTempExample.setRoleId(userRoleTemp.getRoleId());
        userRoleTempExample.setUserId(userRoleTemp.getUserId());
        UserRoleTemp u = userRoleTempDAO.selectOne(userRoleTempExample);
        //根据userId和Role能查到  就更新，查不到就插入，一个人关联角色智能有一条记录
        if (!Objects.isNull(u)) {
            userRoleTemp.setId(u.getId());
            userRoleTempDAO.updateByPrimaryKeySelective(userRoleTemp);
        } else {
            userRoleTemp.setId(UUIDUtils.getUUID());
            userRoleTemp.setCreateTime(new Date());
            userRoleTempDAO.insert(userRoleTemp);
        }
    }


}
